package com.object3D.car
{
	import com.engine.AwayEngine;
	import com.manager.CarMaterial;
	import com.object3D.BaseCar;
	import com.object3D.wheel.AudiR8Wheel;
	import com.utils.PowerLoader;
	
	import away3d.entities.Mesh;
	import away3d.loaders.misc.AssetLoaderContext;
	import away3d.materials.TextureMaterial;
	
	public class AudiR8 extends BaseCar
	{
		public function AudiR8()
		{
			super();
		}
		
		override protected function init():void
		{
			initMaterial();
			loadPNG();
			loadModel();
		}
		
		override protected function loadPNG():void
		{
			PowerLoader.loadFile(carVo.shadowMap,loadMap,shadowMaterial);
		}
		
		override protected function loadModel():void
		{
			PowerLoader.loadFile("models/audi/r8/R8Mtl.xml",loadMtl);
		}
		
		override protected function loadMesh(mesh:Mesh, name:Object=null):void
		{
			mesh.y = FIX_TARGET_Y + 80;
			mesh.x = 85;
			mesh.z = 85;
			mesh.scale(15);
			car3D.addChild(mesh);
			deletionMaterial(mesh)
		}
		
		protected function loadMtl(mtl:Object):void
		{
			var assetLoaderContext:AssetLoaderContext = new AssetLoaderContext(true);
			assetLoaderContext.mapUrlToData("R8.mtl",mtl);
			
			PowerLoader.loadFile(carVo.bodyObjPath,loadMesh,assetLoaderContext);
		}
		
		protected function deletionMaterial(mesh:Mesh):void
		{
			for(var i:int = mesh.numChildren - 1;i >= 0;i--)
			{
				var subMesh:Mesh = mesh.getChildAt(i) as Mesh;
				var subName:String = subMesh.name;
				var subMaterial:TextureMaterial = subMesh.material as TextureMaterial;
				subMaterial.lightPicker = AwayEngine.lightPicker;
				subMaterial.specular = 10;
				subMaterial.addMethod(CarMaterial.bodyReflection);
				if(subName == 'Mesh_0292' || subName == 'Mesh_0294' || subName == 'Mesh_0295' ||
					subName == 'Mesh_0283' || subName == 'Mesh_0285')
				{
					subMesh.material = CarMaterial.glassMaterial;
				}
			}
		}		

		override protected function open():void
		{
			new AudiR8Wheel().show(car3D,carVo.leftForewheel.x,FIX_TARGET_Y + carVo.leftForewheel.y, carVo.leftForewheel.z,false,carVo.wheelObjPath); 
			new AudiR8Wheel().show(car3D,carVo.rightForewheel.x,FIX_TARGET_Y + carVo.rightForewheel.y, carVo.rightForewheel.z,true,carVo.wheelObjPath); 
			new AudiR8Wheel().show(car3D,carVo.leftRearWheel.x,FIX_TARGET_Y + carVo.leftRearWheel.y, carVo.leftRearWheel.z,false,carVo.wheelObjPath); 
			new AudiR8Wheel().show(car3D,carVo.rightRearWheel.x,FIX_TARGET_Y + carVo.rightRearWheel.y, carVo.rightRearWheel.z,true,carVo.wheelObjPath); 
		}
		
	}
}